/* ------------------------------------------------------------------------*
 *
 * ------------------------------------------------------------------------*/

#include "sys_ext.h" 
#include "os_obj.h"
#include "mbapp.h"
#include "bsp_hal.h"
#include "pincfg.h"
#include "com_def.h"

//针对IO模块，超时改为50ms
//RS1
#define mbPORT                       USART2
#define mbURT_IRQn                   USART2_IRQn
#define mbISR                        USART2_IRQHandler
#define RCU_USART                    RCC_APB1PERIPH_USART2
#define RCU_USARTRST                 RCC_APB1PERIPH_USART2
#define mbObj                        mb.obj02
#define mbCmdBuff                    mb.CmdBuff02

static volatile uint8_t alDir;

//RS485 Dir-Pin
#define mbRxEnable()  PAout(1)=0
#define mbTxEnable()  PAout(1)=1

#include "bsp_mbport_common.h"
#include "mb_fun.h"

/*****************************************************************************//*!
*
* @brief   port.
*
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
static void mb_port_hal_cfg(void)
{
    intx_alloc();
    
    intx_disable();

    RCC_APB2PeriphClockCmd(RCU_USART, ENABLE);
    RCC_APB2PeriphResetCmd(RCU_USARTRST, ENABLE);
    RCC_APB2PeriphResetCmd(RCU_USARTRST, DISABLE);

    intx_enable();
}

/*****************************************************************************//*!
*
* @brief   mb object init.
*
* @param   none
*
* @return  TURE
*
* @ Pass/ Fail criteria: none
*****************************************************************************/

static void mb_cb_set(void)
{
	intx_alloc();
	
    mbObj.slave.reg_coils_cb = &smb_reg_coils_cb;
    mbObj.slave.reg_discrete_cb = &smb_reg_discrete_cb;
    mbObj.slave.reg_hold_cb = &smb_reg_holding_cb;
    mbObj.slave.reg_input_cb = &smb_reg_input_cb;

    mmb_cmd_buff_init(&mbObj, mbCmdBuff, 16, 8);

    intx_disable();
	
    //mbObj.api = &mb_fun;
	
    vtask_list_add(&app_tick_task_list, _rtu_timer_run);
    
    intx_enable();
}

/*****************************************************************************//*!
* @brief   	com modle init .
*
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/

void mb02_Init(mb_mode_type mbMode, uint32_t ulBaudRate, mb_parity_type eParity)
{
	// 禁用中断
	nvic_irq_set(mbURT_IRQn,0x0F,0);
	
	// 初始化
   mb_port_Init(mbMode, ulBaudRate, eParity);
}

